//
//  ReversiBoardViewString.m
//  Reversi
//
//  Created by 成瀬 龍人 on 2/4/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ReversiBoardViewString.h"
#import "ReversiController.h"


@implementation ReversiBoardViewString

- (void)startPutAnimation:(ReversiStone)stone:(ReversiBoardPosition*)start:(NSArray*)lineArray{
	willChangePositions = [[NSMutableArray alloc] init];
	[willChangePositions addObject:start];
	for(ReversiBoardLineRange* line in lineArray){
		for(ReversiBoardPosition* pos in line){
			[willChangePositions addObject:pos];
		}
	}
	willChangeStone = stone;
	[putButton setAttributedTitle:[NSString stringWithFormat:@"In animation"]];
}

- (BOOL)updatePutAnimation{
	if (0 == [willChangePositions count]){
		return NO;
	}else{
		ReversiBoardPosition* pos = [willChangePositions objectAtIndex:0];
		[self _put:willChangeStone pos:pos];
		[willChangePositions removeObjectAtIndex:0];
		return YES;
	}
}

- (void)startPassAnimation:(ReversiStone)stone:(NSObject<ReversiPlaying>*)player{
}

- (BOOL)updatePassAnimation{
	return NO;
}

- (void)startInvalidPutPosAnimation:(ReversiStone)stone:(NSObject<ReversiPlaying>*)player{
}

- (BOOL)updateInvalidPutPosAnimation{
	return NO;
}

- (void)changePlayerTo:(NSObject<ReversiPlaying>*)toPlayer
			   toStone:(ReversiStone)toStone
			fromPlayer:(NSObject<ReversiPlaying>*)fromPlayer
			 fromStone:(ReversiStone)fromStone{
	//ボタンのラベルを変える
	[putButton setAttributedTitle:[NSString stringWithFormat:@"Put %@", ReversiStone2String(toStone)]];
	
	//次がユーザが入力するターンなら、入力可能にする
	if ([toPlayer isKindOfClass:[ReversiPlayer class]]){
		[xPositionField setEditable:true];
		[yPositionField setEditable:true];			
		[xPositionField selectText: self];
	}
	//ユーザで無ければ入力不可にしてボタンを選択
	else {
		[xPositionField setEditable:false];
		[yPositionField setEditable:false];	
		[putButton selectCell:nil];
	}
}

- (void)changeToPassState:(NSObject<ReversiPlaying>*)player:(ReversiStone)stone{
	isPassState = true;
	//必ずパスするので、入力不可にする
	[xPositionField setEditable:false];
	[yPositionField setEditable:false];
	//ボタンの文字を"Pass @"か"Pass O"に変更
	[putButton setAttributedTitle:[NSString stringWithFormat:@"Pass %@", ReversiStone2String(stone)]];
	//putボタンをselect状態にする
	[putButton selectCell:nil];
}

- (NSTimeInterval)timerInterval{
	return 0.2;
}

- (id)init{
	[super init];
	board = [[NSMutableString alloc] init];
	[board appendString:@" abcdefgh\n"];
	[board appendString:@"1++++++++\n"];
	[board appendString:@"2++++++++\n"];
	[board appendString:@"3++++++++\n"];
	[board appendString:@"4+++@O+++\n"];
	[board appendString:@"5+++O@+++\n"];
	[board appendString:@"6++++++++\n"];
	[board appendString:@"7++++++++\n"];
	[board appendString:@"8++++++++\n"];
	//[boardField setStringValue:board];
	return self;
}

- (IBAction)put:(id)sender{
	if (isPassState){
		[controller pass];
		isPassState = false;
	}else{
		ReversiBoardPosition* pos = nil;
		@try{
			pos = [[ReversiBoardPosition alloc] initWithString:[xPositionField stringValue]
															  :[yPositionField stringValue]];
		}@catch (ReversiOutOfRangeException* e){
		}
		[controller put:pos];
	}
}

- (id)initWithBoard:(ReversiBoard*)aBoard
		 controller:(id)aController
		blackPlayer:(NSObject<ReversiPlaying>*)black
		whitePlayer:(NSObject<ReversiPlaying>*)white{
	[super init];
	controller = aController;
	isPassState = false;
	board = [[NSMutableString alloc] init];
	[board appendString:@" abcdefgh\n"];
	for(int y = 1; y <= 8; ++y){
		[board appendString:[[NSString alloc] initWithFormat:@"%d",y]];
		for(int x = 1; x <= 8; ++x){
			uint64 pos = int2mask(x, y);
			if([aBoard is:REVERSI_STONE_BLACK mask:pos]){
				[board appendString:@"@"];
			}else if([aBoard is:REVERSI_STONE_WHITE mask:pos]){
				[board appendString:@"O"];
			}else{
				[board appendString:@"+"];
			}
		}
		[board appendString:@"\n"];
	}
	[boardField setStringValue:board];
	
	[putButton setAttributedTitle:[NSString stringWithFormat:@"Put %@", ReversiStone2String(REVERSI_STONE_BLACK)]];
	if ([black isKindOfClass:[ReversiPlayer class]]) {
		[xPositionField setEditable:true];
		[yPositionField setEditable:true];			
		[xPositionField selectText: self];
	}else {
		[xPositionField setEditable:false];
		[yPositionField setEditable:false];			
		[putButton selectCell:nil];
	}	
	return self;
}

- (NSView*)view{
	[NSBundle loadNibNamed:@"StringView"
					 owner:self];
	return mainView;
}

- (void)_put:(ReversiStone)stone pos:(ReversiBoardPosition*)pos{
	int x = 1 + (pos.x - 1);
	int y = 1 + (pos.y - 1);
	int string_pos = y * (8 + 1 + 1) + x;
	NSString* c = stone == REVERSI_STONE_BLACK ? @"@" : @"O";
 	[board replaceCharactersInRange:NSMakeRange(string_pos,1) withString:c];
	[boardField setStringValue:board];
}

@end
